<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/15
 * Time: 17:09
 */

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    //首页
    public function index()
    {
        $data['list'] = $this->goodsList();
        $data['category'] = $this->category();
        $data['home'] = $this->home();
        $data['recommend'] = $this->recommend();
        return $data ? $data:false;
    }
    //商品分页
    public function goodsPage($category_id,$number,$page){
        if (!empty($number)){
            $this->error('参数number不能为空');
        }
        if (!empty($page)){
            $this->error('参数page不能为空');
        }
        $data = $this->goodsList($category_id,$number,$page);
        if (!$data){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }
    //分类商品
    public function categoryGoods($category_id,$number,$page,$sort){
        if (!empty($category_id)){
            $this->error('参数category_id不能为空');
        }
        if (!empty($number)){
            $this->error('参数number不能为空');
        }
        if (!empty($page)){
            $this->error('参数page不能为空');
        }
        $data = $this->goodsList($category_id,$number,$page,$sort);
        if (!$data){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }
    //商品列表
    public function goodsList($category_id=false,$number=10,$page=1,$sort=false){
        switch ($sort){
            case 1;
                $sort = 'id desc';
                break;
            case 2;
                $sort = 'price desc';
                break;
            case 3;
                $sort = 'price asc';
                break;
            case 4;
                $sort = 'sell desc';
                break;
            default;
                $sort = 'sort desc';
        }
        $where = $category_id ? ['category_id'=>$category_id] :'';
        $data = Db::name('goods_spu')->where($where)->order($sort)->limit(($page - 1) * $number,$number)->select();
        foreach ($data as $k=>$v) {
            $data[$k]['category'] = Db::name('goods_category')->where(['id'=>$v['category_id']])->field('id,category_name')->find();
            $data[$k]['image'] = $this->url.$v['image'];
        }
        return $data ? $data:false;
    }
    //首页轮播
    protected function home(){
        $data = Db::name('home_image')->order('sort desc')->select();
        foreach ($data as $k=>$v){
            $data[$k]['image'] = $this->url.$data[$k]['image'];
        }
        return $data ? $data:false;
    }
    //分类
    protected function category(){
        $data = Db::name('goods_category')->order('sort desc')->field('id,category_name')->select();
        return $data ? $data:false;
    }
    //首页推荐分类商品
    protected function recommend(){
        $data = Db::name('goods_category')->where('Recommend',1)->field('id')->select();
        if (!$data){
            return false;
        }
        foreach ($data as $k=>$v){
            $data[$k]['goods'] = Db::name('goods_spu')->where('category_id',$v['id'])->limit(4)->select();
        }
        return $data;
    }
    //商品详情
    public function goodsInfo($goods_id){
            if (!$goods_id){
                $this->error('参数goods_id不能为空');
            }
            $data['goods'] = Db::name('goods_spu')->where(['id'=>$goods_id])->find();
            if (!empty($data)){
                $this->error('无数据');
            }
            $data['goods']['images'] = explode(',',$data['goods']['images']);
            for ($i = 0 ;$i < count($data['goods']['images']);$i++){
                $data['goods']['images'][$i] = $this->url.$data['goods']['images'][$i];
            }
            $data['spec']  = Db::name('goods_spec')->where(['goods_id'=>$goods_id])->select();
            foreach ($data['spec'] as $k=>$v){
                $name = Db::name('spec')->where('id',$v['spec_id'])->find();
                $data['spec'][$k]['spec_id'] = $name;
                $data['spec'][$k]['image'] = $this->url.$data['spec'][$k]['image'];
            }
            $data['commentNumber'] = Db::name('comment')->where(['goods_id'=>$goods_id])->count();
            $this->success('请求成功',$data);
    }





}